package com.umpay.business.common.impl;

import com.umpay.business.common.MgrCommonBusiness;
import com.umpay.dict.DataDict;
import com.umpay.dict.MgrConstant;
import com.umpay.dict.XmlData;
import com.umpay.dict.retcode.MerAccessEC;
import com.umpay.dto.UMFResponse.Message;
import com.umpay.dto.UMFResponse.Message.Meta;
import com.umpay.dto.memberMgr.req.MemberPayAgreementsReq;
import com.umpay.dto.memberMgr.req.MemberQueryReq;
import com.umpay.dto.memberMgr.res.MemberPayAgreementsRes;
import com.umpay.dto.memberMgr.res.MemberQueryRes;
import com.umpay.dto.memberMgr.res.MerInfRes;
import com.umpay.dto.memberMgr.res.vo.PayAgreement;
import com.umpay.exception.BusinessException;
import com.umpay.feign.client.MemberDomainServerFeignClient;
import com.umpay.util.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;


/*******************************************************************
 * description : 请求会员管理实现类
 * @author zhangru
 * @date 2018年2月8日 下午3:13:26
 * @version 1.0
 *******************************************************************/
@Component
public class MgrCommonBusinessImpl implements MgrCommonBusiness {

	private static final Logger log = LogManager.getLogger(MgrCommonBusinessImpl.class);

	@Autowired
	private MemberDomainServerFeignClient memberDomainServerFeignClient;
	
	/**
	 * description : 查询会员信息
	 * @author zhangru
	 * @date 2018年2月7日 下午3:46:13
	 * @version 1.0
	**/
	@Override
	public MemberQueryRes qryMemberInfo(Map<String, String> reqMap){
		MemberQueryReq memberQueryReq = new MemberQueryReq();
		
		if (StringUtils.isNotEmpty(StringUtils.trim(reqMap.get(XmlData.USERID)))) {
			memberQueryReq.setUserId(StringUtils.trim(reqMap.get(XmlData.USERID)));
		} else if (StringUtils.isNotEmpty(StringUtils.trim(reqMap.get(XmlData.MERCUSTID)))) {
			memberQueryReq.setMerCustId(StringUtils.trim(reqMap.get(XmlData.MERCUSTID)));
		} else {
			log.error("【会员信息查询】userId和merCustId必传其一！");
			throw new BusinessException(MerAccessEC.QRY_USERINFO_REQDATAERR, "userId和merCustId必传其一");
		}
		memberQueryReq.setUserType(StringUtils.trim(reqMap.get(XmlData.USERTYPE)));
		memberQueryReq.setMerId(StringUtils.trim(reqMap.get(XmlData.MERID)));
		memberQueryReq.setRpid(StringUtils.trim(reqMap.get(XmlData.RPID)));
		log.info("【会员信息查询】请求参数"+memberQueryReq);
		Message<MemberQueryRes> resMessage = memberDomainServerFeignClient.getMemberInfo(memberQueryReq);
		log.info("【会员信息查询】响应数据"+resMessage);
		if (resMessage == null) {
			throw new BusinessException(MerAccessEC.QUERY_RESULT_UNKNOWN, "查询会员信息失败");
		}
		Meta meta = resMessage.getMeta();
		if (meta == null) {
			throw new BusinessException(MerAccessEC.QUERY_RESULT_UNKNOWN, "查询会员信息失败");
		}
		String retCode = meta.getRet_code();
		String retMsg = meta.getRet_msg();
		if (StringUtils.isEmpty(retCode)) {
			log.error("【会员信息查询】查询结果未明，返回码为空");
			throw new BusinessException(MerAccessEC.QUERY_RESULT_UNKNOWN, "查询会员信息失败");
		} else if (MerAccessEC.SUCCESS.equals(retCode)) {
			MemberQueryRes resInfo = resMessage.getData();
			if (resInfo == null) {
				log.error("【会员信息查询】未查询到相关信息，返回信息为空");
				throw new BusinessException(MerAccessEC.NOT_FIND_MEMBER_INFO, "未查到会员信息");
			} else {
				log.info(String.format("【会员信息查询】会员信息：%s", resInfo));
				String state = StringUtils.trim(resInfo.getState());
				if(!state.equals(MgrConstant.STATE_2)) {
					log.error(String.format("会员状态未开通【state=%s】", state));
					throw new BusinessException(MerAccessEC.NOT_FIND_MEMBER_INFO, "未查到会员信息");
				}
				return resInfo;
			}
		} else {
			log.error(String.format("【会员信息查询】查询会员信息出错,返回结果：%s", resMessage));
			throw new BusinessException(MerAccessEC.NOT_FIND_MEMBER_INFO, "未查到会员信息");
		}
	}

	@Override
	public MemberPayAgreementsRes qryUserBindCardInfo(Map<String, String> reqMap){
		MemberPayAgreementsReq payAgreementsReq = new MemberPayAgreementsReq();
		String merId = StringUtils.trim(reqMap.get(XmlData.MERID));
		String userId = StringUtils.trim(reqMap.get(XmlData.USERID));
		String rpid = StringUtils.trim(reqMap.get(XmlData.RPID));
		String payAgreemenId = StringUtils.trim(reqMap.get(XmlData.PAYAGREEMENID));
		payAgreementsReq.setRpid(rpid);
		payAgreementsReq.setMerId(merId);
		payAgreementsReq.setUserId(userId);
		payAgreementsReq.setPayAgreementId(payAgreemenId);
		log.info("【用户绑卡信息查询】请求参数"+payAgreementsReq);
		Message<MemberPayAgreementsRes> resMessage = memberDomainServerFeignClient.getMemberCardsInf(payAgreementsReq);
		log.info("【用户绑卡信息查询】响应数据"+resMessage);
		if (resMessage == null) {
			throw new BusinessException(MerAccessEC.QUERY_RESULT_UNKNOWN, "查询用户绑卡信息失败");//
		}
		Meta meta = resMessage.getMeta();
		if (meta == null) {
			throw new BusinessException(MerAccessEC.QUERY_RESULT_UNKNOWN, "查询用户绑卡信息失败");//
		}
		String retCode = meta.getRet_code();
		String retMsg = meta.getRet_msg();
		if (StringUtils.isEmpty(retCode)) {
			log.error("【用户绑卡信息查询】查询结果未明");
			throw new BusinessException(MerAccessEC.QUERY_RESULT_UNKNOWN, "查询会员绑卡信息失败");//
		} else if (MerAccessEC.SUCCESS.equals(retCode)) {
			MemberPayAgreementsRes resInfo = resMessage.getData();
			if (resInfo == null) {
				log.error("【用户绑卡信息查询】未查询到相关信息");
				throw new BusinessException(MerAccessEC.BINDCARD_INFO_NULL, "未查到会员绑卡信息");
			} 
			return resInfo;
		} else {
			log.error("【用户绑卡信息查询】查询用户绑卡信息出错！");
			throw new BusinessException(MerAccessEC.BINDCARD_INFO_NULL, "未查到会员绑卡信息");
		}
	}

	@Override
	public MerInfRes qryMerInfo(Map<String, String> reqMap){
		String rpid = StringUtils.trim(reqMap.get(XmlData.RPID));
		String merId = StringUtils.trim(reqMap.get(XmlData.MERID));
		if (StringUtils.isEmpty(merId)) {
			log.error("【平台信息查询】查询平台信息失败，merId is null");
			throw new BusinessException(MerAccessEC.RESULT_UNKNOWN, "查询平台信息失败");
		}
		log.info(String.format("查询平台商户信息请求参数：rpid=%s,merId=%s", rpid,merId));
		Message<MerInfRes> merRes  = memberDomainServerFeignClient.getMerInfo(merId,rpid);
		log.info(String.format("查询平台商户信息响应结果：merRes=%s", merRes));
		if (merRes == null) {
			throw new BusinessException(MerAccessEC.RESULT_UNKNOWN, "查询平台信息失败");
		}
		
		Meta meta = merRes.getMeta();
		if (meta == null) {
			throw new BusinessException(MerAccessEC.RESULT_UNKNOWN, "查询平台信息失败");
		}
		String retCode = meta.getRet_code();
		String retMsg = meta.getRet_msg();
		if (StringUtils.isEmpty(retCode)) {
			log.error("【平台信息查询】查询结果未明");
			throw new BusinessException(MerAccessEC.RESULT_UNKNOWN, "查询平台信息失败");
		} else if (MerAccessEC.SUCCESS.equals(retCode)) {
			MerInfRes merInfo = merRes.getData();
			if (merInfo == null) {
				log.error("【平台信息查询】未查询到相关信息");
				throw new BusinessException(MerAccessEC.MER_NOT_EXIST, "未查到商户信息");
			} else {
				String state = StringUtils.trim(merInfo.getState());
				if (!DataDict.MER_STATE_NORMAL_2.equals(state)) {
					log.error("【平台信息查询】平台状态未开通");
					throw new BusinessException(MerAccessEC.PLATSTATE_CANCEL, "平台状态未开通");
				}
			}
			return merInfo;
		} else {
			log.error("【平台信息查询】查询平台信息出错！");
			throw new BusinessException(retCode, retMsg);
		}
	}
}
